package com.hx.net.b;

import java.io.*;
import java.net.Socket;

/**
 * @author jxlgcmh
 * @create 2019-05-10 21:04
 */
public class TCPClient1 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream out = socket.getOutputStream();
        FileInputStream in = new FileInputStream("a.txt");

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }

        //关闭数据输出，等待服务器的响应
        socket.shutdownOutput();

        //接受服务器发过来的数据
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        byte[] buf1 = new byte[1024];
        int len1;
        while ((len1 = inputStream.read(buf1)) != -1) {
            bas.write(buf1, 0, len1);
        }


        System.out.println(bas.toString());

        //关闭流
        in.close();
        out.close();
        inputStream.close();
        bas.close();

    }
}
